package com.hly.debouncing

import android.os.Bundle
import android.widget.Button
import androidx.activity.ComponentActivity
import androidx.core.content.ContextCompat
import androidx.lifecycle.ViewModelProvider

class SecondActivity : ComponentActivity () {
    private lateinit var viewModel: MyViewModel
    private var button: Button? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.second_activity)
        button = findViewById(R.id.button)
        // 获取 ViewModel 实例
        viewModel = ViewModelProvider(this).get(MyViewModel::class.java)
        // 加载数据
        viewModel.loadData();
        // 单个数据
//        viewModel.data.observe(this) {data->
//            // 监听数据变化
//            button?.text = data

//        }
//        多个数据
        viewModel.data.observe(this) { ButtonBean ->
            // 监听数据变化
            button?.text = ButtonBean.text
            button?.setBackgroundColor(ContextCompat.getColor(this, ButtonBean.backgroundColor))
            button?.setTextSize(ButtonBean.textSize)
            button?.setTextColor(ContextCompat.getColor(this, ButtonBean.textColor))

        }
    }

}